package com.feeyin.radar.crm.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.feeyin.radar.crm.dao.ContractBackCostDAO;
import com.feeyin.radar.crm.model.BackCost;
import com.feeyin.radar.crm.model.BackCostHistory;
import com.feeyin.radar.crm.model.BackCostNew;

public class ContractBackCostDAOImpl extends SqlMapClientDaoSupport
  implements ContractBackCostDAO
{
  public boolean deleteBackCost(Integer id)
  {
    if (id == null) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteBackCost", id));
    return result != null;
  }

  public List getBackCostContractByDynamic(BackCost backCost)
  {
    return getSqlMapClientTemplate().queryForList("getBackCostContractByDynamic", backCost);
  }

  public BigDecimal getYeJiBackCostContractByDynamic(BackCost backCost)
  {
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getYeJiBackCostContractByDynamic", backCost);
  }

  public List getBackCosts(Integer contractId) {
    if (contractId == null) return null;
    return getSqlMapClientTemplate().queryForList("getBackCosts", contractId);
  }

  public BigDecimal getTotalBackCost(Integer contractId) {
    if (contractId == null) return null;
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalBackCost", contractId);
  }

  public BigDecimal getTotalBackCostByDynamic(BackCost backCost) {
    if (backCost == null) return new BigDecimal(0);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalBackCostByDynamic", backCost);
  }

  public boolean insertBackCost(BackCost backCost) {
    if (backCost == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertBackCost", backCost);
    return result != null;
  }

  public boolean updateBackCost(BackCost backCost)
  {
    if ((backCost == null) || (backCost.getId() == null)) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateBackCost", backCost));
    return result != null;
  }
  
  public boolean insertBackCostNew(BackCostNew backCostNew)
  {
	  Integer result = (Integer)getSqlMapClientTemplate().insert("insertBackCostNew", backCostNew);
	  for(BackCostHistory history : backCostNew.getBackCostHistoryList())
	  {
		  history.setBackCostId(result);
		  Integer result1 = (Integer)getSqlMapClientTemplate().insert("insertBackCostHistory", history);
	  }
	  
	  return true;
  }
  
  public boolean isDuplicateBackCostNew(String backCostNum)
  {
	  BackCostNew backCostNew = new BackCostNew();
	  backCostNew.setBackCostNum(backCostNum);
	  
	  List<BackCostNew> results = (List<BackCostNew>)getSqlMapClientTemplate().queryForList("getBackCostNewByDynamic", backCostNew);
	  if(results.size() > 0)
		  return true;
	  else
		  return false;
  }  
  
  public List<BackCostNew> getBackCostNewListByContractId(String contractId)
  {
	  BackCostNew backCostNew = new BackCostNew();
	  backCostNew.setContractId(contractId);
	  
	  return getSqlMapClientTemplate().queryForList("getBackCostNewByDynamic", backCostNew);
  }
  
  public BackCostNew getBackCostNewById(String backCostId)
  {
	  BackCostNew backCostNew = new BackCostNew();
	  backCostNew.setId(Integer.parseInt(backCostId));
	  return (BackCostNew)getSqlMapClientTemplate().queryForObject("getBackCostNewByDynamic", backCostNew);
  }
  
  public boolean updateBackCostNew(BackCostNew backCostNew)
  {
	  Integer result = (Integer)getSqlMapClientTemplate().update("updateBackCostNew", backCostNew);
	  for(BackCostHistory history : backCostNew.getBackCostHistoryList())
	  {
		  getSqlMapClientTemplate().insert("updateBackCostHistory", history);
	  }
	  
	  return true;	  
  }
}